package com.langxin.site.util.tag;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.langxin.site.model.BaseInfo;
import com.langxin.site.model.PageInfo;

/**
 * 分页标签支持
 * @date 2016-04-17
 */
@SuppressWarnings("unchecked")
public class PageTag extends TagSupport{

	private static final long serialVersionUID = 1L;

	//分页参数
	PageInfo<BaseInfo> page = null;
	
	@Override
	public int doStartTag() throws JspException {
		if(page == null){
			return SKIP_BODY;
		}
		//输出的html
		StringBuffer html = new StringBuffer();
		StringBuffer param = new StringBuffer();
		
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();  
		String action = (String)request.getAttribute("javax.servlet.forward.request_uri");
        Enumeration<String> enumeration = request.getParameterNames();
        //请求参数
		while(enumeration.hasMoreElements()){
			String key = enumeration.nextElement();
			if(key.equals("page")){
				continue;
			}
			String value = request.getParameter(key);
			param.append(key + "=" + value).append("&");
		}
		
		// 请求链接
		if(param.length() > 0){
			action = action + "?" + param.toString() + "page=";
		}else{
			action += "?page="; 
		}
		//分页标签view
		html.append("<div id=\"pgt\" class=\"bm bw0 pgs cl\" style=\"margin-left: 18px;float:left;\">");
		html.append("<span id=\"fd_page_top\">");
		html.append("<div class=\"pg\">");
		
		if(this.page.getPage() > 1){
			html.append("  <a rel=\"nofollow\" href=\""+action + this.page.getPrePage() +"\" class=\"pxt\">上一页</a>");
		}
		if(this.page.getTotalPage() < 1){
			html.append("<strong>1</strong>");
		}else if(this.page.getTotalPage() <= 10){
			for(int i = 1; i <= this.page.getTotalPage(); i++){
				if(i == this.page.getPage()){
					html.append("<strong>"+i+"</strong>");
					continue;
				}
				html.append("<a rel=\"nofollow\" href=\""+action+i+"\">"+i+"</a>");
			}
		}else{
			int i = 1;
			int e = this.page.getTotalPage();
			if(this.page.getPage()>6){
				html.append("<a rel=\"nofollow\" href=\""+action+"1\""+" class=\"ib\">1</a>");
				html.append("<a rel=\"nofollow\" href=\"javascript:;\" class=\"ib page-disabled\">...</a>");
				i = this.page.getPage()-3;
			}
			if(this.page.getPage() < this.page.getTotalPage()-3){
				e = this.page.getPage()+3;
			}
			for(; i <= e; i++){
				if(i == this.page.getPage()){
					html.append("<strong>"+i+"</strong>");
					continue;
				}
				html.append("<a rel=\"nofollow\" href=\""+action+i+"\" class=\"ib\">"+i+"</a>");
			}
			if(this.page.getPage() < this.page.getTotalPage()-3){
				html.append("<a href=\"javascript:;\" class=\"ib page-disabled\">...</a>");
				html.append("<a rel=\"nofollow\" href=\""+action+this.page.getTotalPage()+"\" class=\"ib\">"+this.page.getTotalPage()+"</a>");
			}
		}
		
		if(this.page.getPage() < this.page.getTotalPage()){
			html.append("<a rel=\"nofollow\" href=\""+action + this.page.getNextPage()+"\" class=\"nxt\">下一页</a>");
		}
		html.append("</div></div>");
		try {
			//输出分页标签
			pageContext.getOut().print(html.toString());
		} catch (IOException e) {
			throw new JspException(e.getMessage());
		}
		
		return SKIP_BODY;
	}

	public PageInfo<BaseInfo> getPage() {
		return page;
	}

	public void setPage(PageInfo<BaseInfo> page) {
		this.page = page;
	}
}
